// 应用全局状态管理 - Pinia Store
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

/**
 * 应用 Store - 使用 Composition API 风格
 */
export const useAppStore = defineStore('app', () => {
  // 状态
  const isFirstLaunch = ref(true);
  const currentTabIndex = ref(0);
  const networkStatus = ref('wifi');
  const appVersion = ref('1.0.0');
  const systemInfo = ref<any>(null);

  // 计算属性
  const isOnline = computed(() => {
    return networkStatus.value !== 'none';
  });

  const platformInfo = computed(() => {
    if (!systemInfo.value) return { platform: 'unknown', isIOS: false, isAndroid: false };

    const platform = systemInfo.value.platform;
    return {
      platform,
      isIOS: platform === 'ios',
      isAndroid: platform === 'android',
      isDevtools: platform === 'devtools',
    };
  });

  // Actions
  const setFirstLaunch = (value: boolean) => {
    isFirstLaunch.value = value;
    try {
      uni.setStorageSync('isFirstLaunch', value);
    } catch (error) {
      console.error('保存首次启动状态失败:', error);
    }
  };

  const setCurrentTabIndex = (index: number) => {
    currentTabIndex.value = index;
  };

  const updateNetworkStatus = (status: string) => {
    networkStatus.value = status;
  };

  const setSystemInfo = (info: any) => {
    systemInfo.value = info;
  };

  const initAppState = () => {
    try {
      // 恢复首次启动状态
      const localFirstLaunch = uni.getStorageSync('isFirstLaunch');
      if (localFirstLaunch !== '') {
        isFirstLaunch.value = localFirstLaunch;
      }

      // 获取系统信息
      uni.getSystemInfo({
        success: res => {
          setSystemInfo(res);
        },
      });

      // 获取网络状态
      uni.getNetworkType({
        success: res => {
          updateNetworkStatus(res.networkType);
        },
      });

      // 监听网络状态变化
      uni.onNetworkStatusChange(res => {
        updateNetworkStatus(res.networkType);
      });
    } catch (error) {
      console.error('初始化应用状态失败:', error);
    }
  };

  const showToast = (
    title: string,
    icon: 'success' | 'error' | 'none' = 'none',
    duration = 2000
  ) => {
    uni.showToast({
      title,
      icon,
      duration,
    });
  };

  const showLoading = (title = '加载中...') => {
    uni.showLoading({
      title,
      mask: true,
    });
  };

  const hideLoading = () => {
    uni.hideLoading();
  };

  const showModal = (title: string, content: string) => {
    return new Promise<boolean>(resolve => {
      uni.showModal({
        title,
        content,
        success: res => {
          resolve(res.confirm);
        },
        fail: () => {
          resolve(false);
        },
      });
    });
  };

  return {
    // 状态
    isFirstLaunch,
    currentTabIndex,
    networkStatus,
    appVersion,
    systemInfo,

    // 计算属性
    isOnline,
    platformInfo,

    // Actions
    setFirstLaunch,
    setCurrentTabIndex,
    updateNetworkStatus,
    setSystemInfo,
    initAppState,
    showToast,
    showLoading,
    hideLoading,
    showModal,
  };
});
